package com.oscar.practice.shiro.util.lang;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Spring手动获取Bean
 *
 * @author tongyufu
 *
 */
public class SpringContextUtil implements ApplicationContextAware {

	private static ApplicationContext context;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtil.context = applicationContext;
	}

	public static ApplicationContext getContext() {
		return context;
	}

	public static Object getBean(String beanName) {
		return SpringContextUtil.context.getBean(beanName);
	}

	public static <T> T getBean(Class<T> requiredType) {
		return SpringContextUtil.context.getBean(requiredType);
	}

	public static <T> T getBean(String beanName, Class<T> requiredType) {
		return SpringContextUtil.context.getBean(beanName, requiredType);
	}
}
